package control.networking;

import java.util.ArrayList;
import java.util.List;

import model.Player;

/**
 * Keeps track of who is connected to the chat room.
 * Main utility is in being able to broadcast to the
 * group when someone sends a message.
 * 
 * @author Kevin Luikens
 *
 */
public class ConnectedGamers {
	static List<RiskServerHandler> players;
	
	/**
	 * Initializes a ConnectGamers class
	 */
	public ConnectedGamers() {
		players = new ArrayList<RiskServerHandler>();
	}
	
	/**
	 * Adds a gamer to the ConnectedGamer group.
	 * @param rsh RiskServerHandler
	 * @return boolean
	 */
	public boolean addPlayer( RiskServerHandler rsh ) {
		return players.add( rsh );
	}
	
	/**
	 * Removes a gamer from the ConnectedGamers group
	 * @param rsh RiskServerHandler
	 * @return boolean
	 */
	public boolean removePlayer( RiskServerHandler rsh ) {
		return players.remove( rsh );
	}
	
	/**
	 * Returns the size of the gamer group
	 * @return int
	 */
	public int getSize() {
		return players.size();
	}

	/**
	 * Broadcasts the objects to all of the other gamers connected so that
	 * they may update their client with the current Map.
	 * @param o Object
	 */
	public void broadcast(Object o) {
		for ( RiskServerHandler client : players ) {
			client.broadcast( o );
		}
	}
	
	/**
	 * Returns the list of players gaming on the server
	 * @return List<Player>
	 */
	public List<Player> getPlayerList() {
		List<Player> playerList = new ArrayList<Player>();
		
		for ( RiskServerHandler client : players ) {
			playerList.add( client.getPlayer() );
		}
		return playerList;
	}
}
